﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Basics.Param.Group
{
    public partial class List : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        
        protected string _groupcode;
        protected string _groupname; 

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化全局变量值
            _groupcode = Request["groupcode"] == null ? "" : Request["groupcode"];
            _groupname = Request["groupname"] == null ? "" : Request["groupname"];

            //初始化控件值
            tb_search_groupcode.Text = _groupcode;
            tb_search_groupname.Text = _groupname;

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) { RecordCount = GetListCount() };

            if (_framePage.IsAjaxPagerPostBack)
            {
                //分页返回
                BindList(_framePage.PageIndex, _framePage.PageSize);
            }
            else
            {
                //第一次进入页面
                BindList(1, _framePage.PageSize);
            }
        }

        /// <summary>
        /// 填充网格数据
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<SysParam>();
                SetupCondition(ref criteria);
                criteria.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

                grid.DataSource = criteria.List();
                grid.DataBind();
            }
        }

        /// <summary>
        /// 填充查询条件
        /// </summary>
        /// <param name="criteria"></param>
        private void SetupCondition(ref ICriteria criteria)
        {
            criteria.Add(Expression.Eq("GroupCode", "TranferParam")).Add(Expression.Not(Expression.Eq("ItemCode", "TranferParam")));

            if (_groupcode.Trim() != "")
            {
                criteria.Add(Expression.Like("ItemCode", "%" + _groupcode + "%"));
            }
            if (_groupname.Trim() != "")
            {
                criteria.Add(Expression.Like("ItemValue", "%" + _groupname + "%"));
            }
        }

        /// <summary>
        /// 查询数据总数
        /// </summary>
        /// <returns></returns>
        protected int GetListCount()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<SysParam>();
                SetupCondition(ref criteria);
                return (int)criteria.SetProjection(Projections.RowCount()).UniqueResult();
            }
        }
    }
}
